// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedVehicleStory;

import zombie.core.Rand;
import zombie.core.math.PZMath;
import zombie.inventory.InventoryItem;
import zombie.iso.IsoChunk;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMetaGrid;
import zombie.iso.Vector2;
import zombie.vehicles.BaseVehicle;
import zombie.vehicles.VehiclePart;

public final class RVSChangingTire extends RandomizedVehicleStoryBase {
    public RVSChangingTire() {
        this.name = "Changing Tire";
        this.minZoneWidth = 5;
        this.minZoneHeight = 5;
        this.setChance(3);
    }

    @Override
    public void randomizeVehicleStory(IsoMetaGrid.Zone zone, IsoChunk chunk) {
        float _float = (float) (Math.PI / 6);
        this.callVehicleStorySpawner(zone, chunk, Rand.Next(-_float, _float));
    }

    @Override
    public boolean initVehicleStorySpawner(IsoMetaGrid.Zone zone, IsoChunk chunk, boolean debug) {
        VehicleStorySpawner vehicleStorySpawner = VehicleStorySpawner.getInstance();
        vehicleStorySpawner.clear();
        boolean _boolean = Rand.NextBool(2);
        if (debug) {
            _boolean = true;
        }

        int _int = _boolean ? 1 : -1;
        Vector2 vector2 = IsoDirections.N.ToVector();
        vehicleStorySpawner.addElement("vehicle1", (float)_int * -1.5F, 0.0F, vector2.getDirection(), 2.0F, 5.0F);
        vehicleStorySpawner.addElement("tire1", (float)_int * 0.0F, 0.0F, 0.0F, 1.0F, 1.0F);
        vehicleStorySpawner.addElement("tool1", (float)_int * 0.8F, -0.2F, 0.0F, 1.0F, 1.0F);
        vehicleStorySpawner.addElement("tool2", (float)_int * 1.2F, 0.2F, 0.0F, 1.0F, 1.0F);
        vehicleStorySpawner.addElement("tire2", (float)_int * 2.0F, 0.0F, 0.0F, 1.0F, 1.0F);
        vehicleStorySpawner.setParameter("zone", zone);
        vehicleStorySpawner.setParameter("removeRightWheel", _boolean);
        return true;
    }

    @Override
    public void spawnElement(VehicleStorySpawner spawner, VehicleStorySpawner.Element element) {
        IsoGridSquare isoGridSquare = element.square;
        if (isoGridSquare != null) {
            float float0 = PZMath.max(element.position.x - (float)isoGridSquare.x, 0.001F);
            float float1 = PZMath.max(element.position.y - (float)isoGridSquare.y, 0.001F);
            float float2 = 0.0F;
            float float3 = element.z;
            IsoMetaGrid.Zone zone = spawner.getParameter("zone", IsoMetaGrid.Zone.class);
            boolean _boolean = spawner.getParameterBoolean("removeRightWheel");
            BaseVehicle baseVehicle = spawner.getParameter("vehicle1", BaseVehicle.class);
            String string = element.id;
            switch (string) {
                case "tire1":
                    if (baseVehicle != null) {
                        InventoryItem inventoryItem1 = isoGridSquare.AddWorldInventoryItem(
                            "Base.ModernTire" + baseVehicle.getScript().getMechanicType(), float0, float1, float2
                        );
                        if (inventoryItem1 != null) {
                            inventoryItem1.setItemCapacity((float)inventoryItem1.getMaxCapacity());
                        }

                        this.addBloodSplat(isoGridSquare, Rand.Next(10, 20));
                    }
                    break;
                case "tire2":
                    if (baseVehicle != null) {
                        InventoryItem inventoryItem0 = isoGridSquare.AddWorldInventoryItem(
                            "Base.OldTire" + baseVehicle.getScript().getMechanicType(), float0, float1, float2
                        );
                        if (inventoryItem0 != null) {
                            inventoryItem0.setCondition(0);
                        }
                    }
                    break;
                case "tool1":
                    isoGridSquare.AddWorldInventoryItem("Base.LugWrench", float0, float1, float2);
                    break;
                case "tool2":
                    isoGridSquare.AddWorldInventoryItem("Base.Jack", float0, float1, float2);
                    break;
                case "vehicle1":
                    baseVehicle = this.addVehicle(zone, element.position.x, element.position.y, float3, element.direction, "good", null, null, null);
                    if (baseVehicle != null) {
                        baseVehicle.setGeneralPartCondition(0.7F, 40.0F);
                        baseVehicle.setRust(0.0F);
                        VehiclePart vehiclePart = baseVehicle.getPartById(_boolean ? "TireRearRight" : "TireRearLeft");
                        baseVehicle.setTireRemoved(vehiclePart.getWheelIndex(), true);
                        vehiclePart.setModelVisible("InflatedTirePlusWheel", false);
                        vehiclePart.setInventoryItem(null);
                        this.addZombiesOnVehicle(2, null, null, baseVehicle);
                        spawner.setParameter("vehicle1", baseVehicle);
                    }
            }
        }
    }
}
